package com.siwei.mes.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.exception.BizException;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.Order;
import com.siwei.mes.entity.OrderDetail;
import com.siwei.mes.entity.orderDetail.*;
import com.siwei.mes.mapper.OrderDetailMapper;
import com.siwei.mes.service.OrderDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 工单详情(OrderDetail)表服务实现类
 *
 * @author makejava
 * @since 2023-12-05 09:31:35
 */
@Slf4j
@Service
public class OrderDetailServiceImpl implements OrderDetailService {
    @Resource
    private OrderDetailMapper orderDetailMapper;

    @Override
    public Long create(OrderDetail entity) {
        orderDetailMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(OrderDetail entity) {
        OrderDetail orderDetail = orderDetailMapper.selectByPrimaryKey(entity.getId());
        assertTrue(orderDetail == null, "工单详情信息不存在");
        orderDetailMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        OrderDetail orderDetail = orderDetailMapper.selectByPrimaryKey(id);
        assertTrue(orderDetail == null, "工单详情信息不存在");
        orderDetailMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<OrderDetail> getPage(PageQuery<OrderDetail, OrderDetail> query) {
        OrderDetail searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> orderDetailMapper.getList(searchParam));
    }

    @Override
    public OrderDetail loadById(Long id) {
        return orderDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<OrderDetail> getAll(OrderDetail entity) {
        return orderDetailMapper.getList(entity);
    }

    /**
     * 获取工单对应的json
     *
     * @param orderTypeCode 工单类型
     * @return
     */
    public JSONObject getJsonObject(String orderTypeCode, JSONObject detailJson) {
        /**获取对应的jsonObject 对象*/
        JSONObject jsonObject = new JSONObject();
        switch (orderTypeCode) {
            case "JCGJ":
                jsonObject = JCGJDetail.toJsonObject(detailJson);
                break;
            case "QDHT":
                jsonObject = QDHTDetail.toJsonObject(detailJson);
                break;
            case "XCFK":
                jsonObject = XCFKDetail.toJsonObject(detailJson);
                break;
            case "XMHF":
                jsonObject = XMHFDetail.toJsonObject(detailJson);
                break;
            case "ZLSG":
                jsonObject = ZLSGDetail.toJsonObject(detailJson);
                break;
            case "ZLTS":
                jsonObject = ZLTSDetail.toJsonObject(detailJson);
                break;
            default:
                log.info("未知类型：" + orderTypeCode);
        }
        return jsonObject;
    }

    @Override
    public OrderDetail insertDetail(Order entity, boolean isAdd) {
        String orderTypeCode = entity.getOrderTypeCode();
        JSONObject detailJsonInfo = entity.getDetailInfo();
        JSONObject detailFileInfo = entity.getDetailFileInfo();
        /**这里根据工单id查询是否有详情数据*/
        OrderDetail detailInfo = orderDetailMapper.selectByOrderId(entity.getId());

        /**这里判断如果是修改并且也没有传工单详情对应的数据 直接赋值之前的详情数据*/
        if (!isAdd && (detailJsonInfo == null || detailJsonInfo.isEmpty())) {
            detailJsonInfo = detailInfo.getDetailJson();
        }
        if (!isAdd && (detailFileInfo == null || detailFileInfo.isEmpty())) {
            detailFileInfo = detailInfo.getDetailFile();
        }

        /**获取对应的jsonObject 对象*/
        JSONObject detailJson = this.getJsonObject(orderTypeCode, detailJsonInfo);
        if (detailJson == null) {
            throw new BizException("未知工单类型：" + orderTypeCode);
        }
        JSONObject detailFile = DetailFile.toJsonObject(detailFileInfo);

        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setId(null);
        orderDetail.setOrderId(entity.getId());
        orderDetail.setOrderTypeCode(entity.getOrderTypeCode());
        orderDetail.setOrderTypeName(entity.getOrderTypeName());
        orderDetail.setDetailJson(detailJson);
        orderDetail.setDetailFile(detailFile);
        orderDetail.init(true);
        if (detailInfo == null) {
            orderDetailMapper.insertSelective(orderDetail);
        } else {
            orderDetail.setId(detailInfo.getId());
            orderDetailMapper.updateByPrimaryKeySelective(orderDetail);
        }
        return orderDetail;
    }

    @Override
    public OrderDetail payloadDetail(Order order) {
        OrderDetail orderDetail = order.getOrderDetail();
        if (orderDetail == null) {
            log.info("工单详情数据不存在，直接新增一个");
            orderDetail = new OrderDetail();
            orderDetail.setOrderId(order.getId());
            orderDetail.setOrderTypeCode(order.getOrderTypeCode());
            orderDetail.init(true);
            orderDetail.setDetailJson(this.getJsonObject(order.getOrderTypeCode(), null));
            orderDetail.setDetailFile(DetailFile.toJsonObject(null));
            orderDetailMapper.insertSelective(orderDetail);
            return orderDetail;
        }
        String orderTypeCode = orderDetail.getOrderTypeCode();
        JSONObject detailJson = orderDetail.getDetailJson();
        JSONObject detailFile = orderDetail.getDetailFile();

        orderDetail.setDetailJson(this.getJsonObject(orderTypeCode, detailJson));
        orderDetail.setDetailFile(DetailFile.toJsonObject(detailFile));
        return orderDetail;
    }
}
